﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Animation;

namespace Xiaowei.Controls
{
    public partial class ActiveButton
    {

        private List<Storyboard> currentAnimationList;
        private List<Storyboard> expandStoryList1;
        private List<Storyboard> expandStoryList2;
        private List<Storyboard> reduceStoryList1;
        private int currentAnimationIndex;

        void InitAnimation()
        {
            expandStoryList1 = new List<Storyboard>();
            reduceStoryList1 = new List<Storyboard>();
            expandStoryList2 = new List<Storyboard>();
            //DoubleAnimation animation1 = new DoubleAnimation();
            //animation1.From = 200;
            //animation1.To = 0;
            //animation1.Duration = TimeSpan.FromSeconds(0.3);
            //animation1.FillBehavior = FillBehavior.Stop;
            //Storyboard.SetTarget(animation1,activeButton);
            //Storyboard.SetTargetProperty(animation1,new PropertyPath("PositionAnimationKeyValue"));
            //Storyboard story1 = new Storyboard();
            //story1.Children.Add(animation1);
            //animation1.Completed += (s,e)=> {
            //    SetValue(PositionAnimationKeyValueProperty,0d);
            //    PlayNextAnimation();
            //};
            //expandStoryList1.Add(story1);

            DoubleAnimation animation1 = new DoubleAnimation();
            animation1.From = 0;
            animation1.To = 1;
            animation1.Duration = TimeSpan.FromSeconds(0.2);
            animation1.FillBehavior = FillBehavior.Stop;
            Storyboard.SetTarget(animation1, activeButton);
            Storyboard.SetTargetProperty(animation1, new PropertyPath("SizeAnimationKeyValue"));
            Storyboard story1 = new Storyboard();
            story1.Children.Add(animation1);
            animation1.Completed += (s, e) => {
                SetValue(SizeAnimationKeyValueProperty, 1d);
                PlayNextAnimation();
            };
            expandStoryList1.Add(story1);
            expandStoryList2.Add(story1);
            animation1 = new DoubleAnimation();
            animation1.From = 0;
            animation1.To = 1;
            animation1.Duration = TimeSpan.FromSeconds(0.1);
            animation1.FillBehavior = FillBehavior.Stop;
            Storyboard.SetTarget(animation1, activeButton);
            Storyboard.SetTargetProperty(animation1, new PropertyPath("RectAnimation1KeyValue"));
            story1 = new Storyboard();
            story1.Children.Add(animation1);
            animation1.Completed += (s, e) => {
                SetValue(RectAnimation1KeyValueProperty, 1d);
                PlayNextAnimation();
            };
            expandStoryList1.Add(story1);
            expandStoryList2.Add(story1);

            animation1 = new DoubleAnimation();
            animation1.From = 0;
            animation1.To = 1;
            animation1.Duration = TimeSpan.FromSeconds(0.1);
            animation1.FillBehavior = FillBehavior.Stop;
            Storyboard.SetTarget(animation1, activeButton);
            Storyboard.SetTargetProperty(animation1, new PropertyPath("RectAnimation2KeyValue"));
            story1 = new Storyboard();
            story1.Children.Add(animation1);
            animation1.Completed += (s, e) => {
                SetValue(RectAnimation2KeyValueProperty, 1d);
                PlayNextAnimation();
            };
            expandStoryList1.Add(story1);
            expandStoryList2.Add(story1);

            animation1 = new DoubleAnimation();
            animation1.From = 1;
            animation1.To = 0;
            animation1.Duration = TimeSpan.FromSeconds(0.1);
            animation1.FillBehavior = FillBehavior.Stop;
            Storyboard.SetTarget(animation1, activeButton);
            Storyboard.SetTargetProperty(animation1, new PropertyPath("RectAnimation2KeyValue"));
            story1 = new Storyboard();
            story1.Children.Add(animation1);
            animation1.Completed += (s, e) => {
                SetValue(RectAnimation2KeyValueProperty, 0d);
                PlayNextAnimation();
            };
            reduceStoryList1.Add(story1);

            animation1 = new DoubleAnimation();
            animation1.From = 1;
            animation1.To = 0;
            animation1.Duration = TimeSpan.FromSeconds(0.1);
            animation1.FillBehavior = FillBehavior.Stop;
            Storyboard.SetTarget(animation1, activeButton);
            Storyboard.SetTargetProperty(animation1, new PropertyPath("RectAnimation1KeyValue"));
            story1 = new Storyboard();
            story1.Children.Add(animation1);
            animation1.Completed += (s, e) => {
                SetValue(RectAnimation1KeyValueProperty, 0d);
                PlayNextAnimation();
            };
            reduceStoryList1.Add(story1);

            animation1 = new DoubleAnimation();
            animation1.From = 1;
            animation1.To = 0;
            animation1.Duration = TimeSpan.FromSeconds(0.2);
            animation1.FillBehavior = FillBehavior.Stop;
            Storyboard.SetTarget(animation1, activeButton);
            Storyboard.SetTargetProperty(animation1, new PropertyPath("SizeAnimationKeyValue"));
            story1 = new Storyboard();
            story1.Children.Add(animation1);
            animation1.Completed += (s, e) => {
                SetValue(SizeAnimationKeyValueProperty, 0d);
                PlayNextAnimation();
                SwitchToNormalEnd?.Invoke();
            };
            reduceStoryList1.Add(story1);
        }
        public static DependencyProperty PositionAnimationKeyValueProperty = DependencyProperty.Register(
            "PositionAnimationKeyValue", typeof(double), typeof(ActiveButton), new PropertyMetadata(200d, PositionAnimationKeyValuePropertyChanged)
            );
        public static void PositionAnimationKeyValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ActiveButton sender = (ActiveButton)d;
            sender.Margin = new Thickness((double)e.NewValue, 10, -(double)e.NewValue, 12);
        }
        public static DependencyProperty SizeAnimationKeyValueProperty = DependencyProperty.Register(
            "SizeAnimationKeyValue", typeof(double), typeof(ActiveButton), new PropertyMetadata(0d, SizeAnimationKeyValuePropertyChanged)
            );
        public static void SizeAnimationKeyValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ActiveButton sender = (ActiveButton)d;
            sender.border.Width = 46 + ((double)e.NewValue) * (144 - 46);
        }

        public static DependencyProperty RectAnimation1KeyValueProperty = DependencyProperty.Register(
            "RectAnimation1KeyValue", typeof(double), typeof(ActiveButton), new PropertyMetadata(0d, RectAnimation1KeyValuePropertyChanged)
            );
        public static void RectAnimation1KeyValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ActiveButton sender = (ActiveButton)d;
            sender.row1.Height = new GridLength(6 + ((double)e.NewValue) * (0 - 6));
        }

        public static DependencyProperty RectAnimation2KeyValueProperty = DependencyProperty.Register(
            "RectAnimation2KeyValue", typeof(double), typeof(ActiveButton), new PropertyMetadata(0d, RectAnimation2KeyValuePropertyChanged)
            );
        public static void RectAnimation2KeyValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ActiveButton sender = (ActiveButton)d;
            sender.col1.Width = new GridLength(0 + ((double)e.NewValue) * (28 - 0));
            sender.col2.Width = new GridLength(0 + ((double)e.NewValue) * (28 - 0));
            sender.col3.Width = new GridLength(4 + ((double)e.NewValue) * (0 - 4));
            sender.col4.Width = new GridLength(0 + ((double)e.NewValue) * (28 - 0));
            sender.col5.Width = new GridLength(0 + ((double)e.NewValue) * (28 - 0));
        }

        private void PlayNextAnimation()
        {
            if(currentAnimationList==null)
            {
                return;
            }

            currentAnimationIndex++;
            if(currentAnimationList.Count> currentAnimationIndex)
            {
                currentAnimationList[currentAnimationIndex].Begin();
            }
        }

        public void PlayExpandAnimation1()
        {
            if (currentAnimationList != null)
            {
                if (currentAnimationList.Count > currentAnimationIndex)
                {
                    currentAnimationList[currentAnimationIndex].Stop();
                }
            }
            currentAnimationList = expandStoryList1;
            currentAnimationIndex = 0;
            expandStoryList1[0].Begin();
        }
        public void PlayExpandAnimation2()
        {
            if (currentAnimationList != null)
            {
                if (currentAnimationList.Count > currentAnimationIndex)
                {
                    currentAnimationList[currentAnimationIndex].Stop();
                }
            }
            currentAnimationList = expandStoryList2;
            currentAnimationIndex = 0;
            expandStoryList2[0].Begin();
        }
        public void PlayReduceStoryList1()
        {
            if (currentAnimationList != null)
            {
                if (currentAnimationList.Count > currentAnimationIndex)
                {
                    currentAnimationList[currentAnimationIndex].Stop();
                }
            }
            currentAnimationList = reduceStoryList1;
            currentAnimationIndex = 0;
            reduceStoryList1[0].Begin();
        }

    }
}
